package com.whlilong.jxc.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;

import com.whlilong.jxc.print.DistrictTableContentProvider;
import com.whlilong.jxc.print.DistrictTableLableProvider;
import com.whlilong.jxc.print.IconSource;
import com.whlilong.jxc.print.Keshang;
import com.whlilong.jxc.print.PDocument;
import com.whlilong.jxc.print.PHLine;
import com.whlilong.jxc.print.PTableBoxProvider;
import com.whlilong.jxc.print.PTextBox;
import com.whlilong.jxc.print.PVSpace;
import com.whlilong.jxc.print.PrintPreview;
import com.whlilong.jxc.print.SWTPTable;

public class PrintInfoDialog extends TrayDialog {

	private List input;
	private Table tablev;

	public PrintInfoDialog(Shell parentShell, List input) {
		// TODO Auto-generated constructor stub
		super(parentShell);
		this.input = input;
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		TableViewer tableViewer = new TableViewer(composite, SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);

		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 4;
		tablev = tableViewer.getTable();
		tablev.setLayoutData(gd);
		tablev.setToolTipText("点击单元格对数据进行修改！");
		// 设置行高
		tablev.addListener(SWT.MeasureItem, new Listener() {
			@Override
			public void handleEvent(Event event) {
				// TODO Auto-generated method stub
				event.height = 20;
			}
		});
		tablev.setHeaderVisible(true);
		tablev.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(tablev, SWT.NONE);
		tableColumn.setText("ID");
		tableColumn.setWidth(50);
		TableColumn tablColumn = new TableColumn(tablev, SWT.NONE);
		tablColumn.setText("name");
		tablColumn.setWidth(50);

		tableViewer.setContentProvider(new DistrictTableContentProvider());
		tableViewer.setLabelProvider(new DistrictTableLableProvider());

		tableViewer.setInput(Keshang.sample());
		return null;

	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("信息打印");
	}

	protected void createButtonsForButtonBar(Composite parent) {
		// create OK and Cancel buttons by default
		createButton(parent, IDialogConstants.OK_ID, "打印预览(&P)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);

	}

	protected void buttonPressed(int buttonId) {
		if (IDialogConstants.OK_ID == buttonId) {
			okPressed();
		} else if (IDialogConstants.CANCEL_ID == buttonId) {
			cancelPressed();
		}
	}

	protected void okPressed() {
		final PDocument doc = new PDocument("print");

		// put some header text on it
		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable table = new SWTPTable(doc);
		table.setTable(tablev);
		table.setBoxProvider(new PTableBoxProvider());
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.open();
		
	}

}
